package com.ostrich.library_net

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/24 09:56
 * @Description : RetrofitManager
 */
class RetrofitManager {

    companion object{
        //双重校验锁单例
        val instance:RetrofitManager by lazy(LazyThreadSafetyMode.SYNCHRONIZED){
            RetrofitManager()
        }
    }

    private val retrofit:Retrofit
    private val rxjavaRetrofit:Retrofit
    //工厂模式创建不同的service
    init {
        retrofit = Retrofit.Builder()
            .client(createOkhttpClient())
            .baseUrl("http://baobab.kaiyanapp.com")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(LivedataCallAdapterFactory.create())
            .build()

        rxjavaRetrofit = Retrofit.Builder()
            .client(createOkhttpClient())
            .baseUrl("http://baobab.kaiyanapp.com")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }

    private fun createOkhttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(60,TimeUnit.SECONDS)
            .writeTimeout(60,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }

    fun <T> createRxService(class_:Class<T>):T{
        return rxjavaRetrofit.create(class_)
    }



    fun <T> createService(class_:Class<T>):T{
       return retrofit.create(class_)
    }






}